package com.css.fxfzdzzh.modules.mapManagement.controller;

import com.css.fxfzdzzh.base.response.RestResponse;
import com.css.fxfzdzzh.modules.mapManagement.param.MapQueryParam;
import com.css.fxfzdzzh.modules.mapManagement.service.MapService;
import com.css.fxfzdzzh.modules.mapManagement.vo.MapVO;
import com.css.fxfzdzzh.util.PlatformObjectUtils;
import com.css.fxfzdzzh.util.PlatformPageUtils;
import com.css.fxfzdzzh.util.ResponseCreator;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author Dbj
 * @date 2023年05月16日  10:22
 */
@RestController
@RequestMapping("/map")
@Validated
public class MapController {

    @Resource
    MapService mapService;

    /**
     * 图件上传保存
     */
    @PostMapping("/uploadMap")
    public RestResponse uploadMap(@Valid @RequestBody MapVO mapVO){
        RestResponse response = null;
        try{
            mapService.uploadMap(mapVO);
            response=ResponseCreator.createSuccessResponse("图件上传成功");
        }catch (Exception e){
            String errorMessage = "上传失败!";
            response=ResponseCreator.createFailResponse(errorMessage);
        }
        return response;
    }

    /**
     *  图件分页查询
     */
    @GetMapping("/mapPage")
    public RestResponse mapPage(MapQueryParam param, HttpServletRequest request){
        int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
        int pageSize=Integer.parseInt(request.getParameter(PlatformPageUtils.PAGE_SIZE));
        Map<String,Object> page=mapService.mapPage(param,curPage,pageSize);
        return ResponseCreator.createSuccessResponse(page);
    }

    /**
     *  图件批量删除(勾选)
     */
    @PostMapping("/delete")
    public RestResponse delete(@RequestBody Map<String,Object> map ) throws Exception {
        List<String> dmIdList = (List<String>) map.get("dmIdList");
        if(PlatformObjectUtils.isEmpty(dmIdList) || dmIdList.size()==0){
            throw new RuntimeException("请选择要删除的图件信息!");
        }
        mapService.delete(dmIdList);
        return ResponseCreator.createSuccessResponse("删除成功!");
    }

    /**
     *  图件批量下载(勾选)
     */
    @PostMapping("/download")
    public void download(@RequestBody Map<String,Object> map, HttpServletResponse resp) throws Exception {
        List<String> attacheIdList = (List<String>) map.get("attachIdList");
        if(PlatformObjectUtils.isEmpty(attacheIdList) || attacheIdList.size()==0){
            throw new RuntimeException("请选择要下载的图件信息!");
        }
        mapService.download(attacheIdList,resp);
    }

    /**
     *  查询任务列表
     */
    @GetMapping("/getTaskList")
    public RestResponse getTaskList(){
        return ResponseCreator.createSuccessResponse( mapService.getTaskList());
    }

    /**
     *  查询地表破裂带编号列表
     */
    @GetMapping("/getRuptureList")
    public RestResponse getRuptureList(){
        return ResponseCreator.createSuccessResponse( mapService.getRuptureList());
    }

    /**
     *  图件回显
     */
    @GetMapping("/mapView")
    public RestResponse mapView(@RequestParam String dmId){
        return ResponseCreator.createSuccessResponse(mapService.getById(dmId));
    }


}
